package org.Yiran.timetale_re.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import org.Yiran.timetale_re.Timetale_re;
import org.Yiran.timetale_re.menu.RuneBlueprintBookMenu;
import org.jetbrains.annotations.NotNull;

/**
 * 符文蓝图手册屏幕
 * 用于显示和管理符文蓝图的GUI界面
 * 继承自AbstractContainerScreen，提供自定义的渲染逻辑
 */
public class RuneBlueprintBookScreen extends AbstractContainerScreen<RuneBlueprintBookMenu> {
    
    // ==================== 纹理资源定义 ====================
    
    /** GUI纹理资源位置 */
    private static final ResourceLocation GUI_TEXTURE = ResourceLocation.fromNamespaceAndPath(Timetale_re.MODID, "textures/gui/rune_blueprint_book.png");

    // ==================== 槽位贴图UV坐标定义 ====================
    
    /** 槽位贴图在纹理中的U坐标 */
    private static final int SLOT_U = 7;
    
    /** 槽位贴图在纹理中的V坐标 */
    private static final int SLOT_V = 83;
    
    /** 槽位贴图宽度 */
    private static final int SLOT_WIDTH = 18;
    
    /** 槽位贴图高度 */
    private static final int SLOT_HEIGHT = 18;

    // ==================== 构造函数 ====================

    /**
     * 构造函数
     * @param menu 符文蓝图手册菜单
     * @param playerInventory 玩家物品栏
     * @param title 屏幕标题
     */
    public RuneBlueprintBookScreen(RuneBlueprintBookMenu menu, Inventory playerInventory, Component title) {
        super(menu, playerInventory, title);
        this.imageWidth = 176;
        this.imageHeight = 166;
    }

    // ==================== 背景渲染方法 ====================

    /**
     * 渲染GUI背景
     * @param guiGraphics GUI图形绘制对象
     * @param partialTick 部分刻度
     * @param mouseX 鼠标X坐标
     * @param mouseY 鼠标Y坐标
     */
    @Override
    protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, int mouseY) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, GUI_TEXTURE);
        int x = (width - imageWidth) / 2;
        int y = (height - imageHeight) / 2;
        guiGraphics.blit(GUI_TEXTURE, x, y, 0, 0, imageWidth, imageHeight);

        // 为每个手册槽位渲染相同地贴图
        renderSlotOverlays(guiGraphics, x, y);
    }

    /**
     * 为手册中的每个槽位渲染相同的贴图
     * @param guiGraphics GUI图形绘制对象
     * @param guiLeft GUI左上角X坐标
     * @param guiTop GUI左上角Y坐标
     */
    private void renderSlotOverlays(GuiGraphics guiGraphics, int guiLeft, int guiTop) {
        // 渲染9个手册槽位的贴图（使用相同贴图）
        for (int i = 0; i < RuneBlueprintBookMenu.BOOK_SLOTS; i++) {
            Slot slot = menu.getSlot(i);

            // 获取槽位的屏幕坐标
            int slotX = guiLeft + slot.x - 1;
            int slotY = guiTop + slot.y - 1;

            // 所有槽位使用相同的纹理坐标
            guiGraphics.blit(GUI_TEXTURE, slotX, slotY, SLOT_U, SLOT_V, SLOT_WIDTH, SLOT_HEIGHT);
        }
    }

    // ==================== 主渲染方法 ====================

    /**
     * 主渲染方法
     * @param guiGraphics GUI图形绘制对象
     * @param mouseX 鼠标X坐标
     * @param mouseY 鼠标Y坐标
     * @param partialTick 部分刻度
     */
    @Override
    public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        renderBackground(guiGraphics);
        super.render(guiGraphics, mouseX, mouseY, partialTick);
        renderTooltip(guiGraphics, mouseX, mouseY);
    }
}